/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.zhpan.bannerview.loader;

import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import com.zhpan.bannerview.Log;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * ImageLoader
 *
 * @since 2021-03-24
 */
public abstract class ImageLoader implements ImageLoaderInterface<Image> {
    private static final int CONFIGINT = 10;

    /**
     * createImageView
     *
     * @param context 參數
     * @param path 參數
     * @return Image
     */
    @Override
    public Image createImageView(Context context, Object path) {
        Image imageView = new Image(context);
        String urlImage = "https://www.harmonyos.com/resource/image/community/20201009-164134eSpace.jpg";
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(urlImage);
            URLConnection urlConnection = url.openConnection();
            if (urlConnection instanceof HttpURLConnection) {
                connection = (HttpURLConnection) urlConnection;
            }
            if (connection != null) {
                connection.connect();
                inputStream = urlConnection.getInputStream(); // 之后可进行url的其他操作,得到服务器返回过来的流对象
                ImageSource imageSource = ImageSource.create(inputStream, new ImageSource.SourceOptions());
                ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
                decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
                PixelMap pixelMap = imageSource.createPixelmap(decodingOptions); // 普通解码叠加旋转、缩放、裁剪
                DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
                    DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                    DirectionalLayout.LayoutConfig.MATCH_CONTENT); // 普通解码
                config.setMargins(CONFIGINT, CONFIGINT, CONFIGINT, CONFIGINT);
                imageView.setLayoutConfig(config);
                imageView.setPixelMap(pixelMap);
                pixelMap.release();
                inputStream.close();
            }
        } catch (MalformedURLException ex) {
            Log.debug(ex.getMessage());
        } catch (IOException ex) {
            Log.debug(ex.getMessage());
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return imageView;
    }
}
